/**
 * 
 */

package com.applabs.mydiary.core;

import com.applabs.mydiary.provider.DiaryEventProvider;
import com.applabs.mydiary.provider.DiaryEventTable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * @author praveenthota
 */
public class DbPersister implements IPersister {

    Context mContext;

    public static DbPersister sDbPersister = null;

    private DiaryEventProvider mDbProvider;

    public static synchronized DbPersister getInstance(Context context) {
        if (sDbPersister == null) {
            sDbPersister = new DbPersister(context);
        }
        return sDbPersister;
    }

    private DbPersister(Context context) {
        mContext = context;
        mDbProvider = new DiaryEventProvider(context);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.applabs.mydiary.core.IPersister#addEvent(com.applabs.mydiary.core
     * .DiaryEvent)
     */
    @Override
    public boolean addEvent(DiaryEvent event) {
        // TODO Auto-generated method stub
        ContentValues values = new ContentValues();
        values.put(DiaryEventTable.COLUMN_EVT_STIME, event.getStartTime().toString());
        values.put(DiaryEventTable.COLUMN_EVT_ETIME, event.getEndTime().toString());
        values.put(DiaryEventTable.COLUMN_EVT_LATI, event.getLatitude());
        values.put(DiaryEventTable.COLUMN_EVT_LATI, event.getLongitude());
        values.put(DiaryEventTable.COLUMN_EVT_NOTES, event.getNotes());
        values.put(DiaryEventTable.COLUMN_EVT_ADDR, event.getAddress());
        values.put(DiaryEventTable.COLUMN_EVT_DATE, event.getEventDate());
        mDbProvider.insert(DiaryEventProvider.CONTENT_URI, values);

        return true;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.applabs.mydiary.core.IPersister#delEvent(com.applabs.mydiary.core
     * .DiaryEvent)
     */
    @Override
    public boolean delEvent(DiaryEvent event) {
        // TODO Auto-generated method stub
        Uri uri = Uri.parse(mDbProvider.CONTENT_URI + "/" + event.getEventId());
        mDbProvider.delete(uri, null, null);
        return true;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.applabs.mydiary.core.IPersister#updateEvent(com.applabs.mydiary.core
     * .DiaryEvent)
     */
    @Override
    public boolean updateEvent(DiaryEvent event) {
        // TODO Auto-generated method stub
        ContentValues values = new ContentValues();
        values.put(DiaryEventTable.COLUMN_EVT_STIME, event.getStartTime().toString());
        values.put(DiaryEventTable.COLUMN_EVT_ETIME, event.getEndTime().toString());
        values.put(DiaryEventTable.COLUMN_EVT_LATI, event.getLatitude());
        values.put(DiaryEventTable.COLUMN_EVT_LATI, event.getLongitude());
        values.put(DiaryEventTable.COLUMN_EVT_NOTES, event.getNotes());
        values.put(DiaryEventTable.COLUMN_EVT_ADDR, event.getAddress());
        values.put(DiaryEventTable.COLUMN_EVT_DATE, event.getEventDate().toString());
        Uri uri = Uri.parse(mDbProvider.CONTENT_URI + "/" + event.getEventId());
        mDbProvider.update(uri, values, null, null);
        return false;
    }

    /*
     * (non-Javadoc)
     * @see com.applabs.mydiary.core.IPersister#initialize()
     */
    @Override
    public boolean initialize() {
        // TODO Auto-generated method stub
        return true;
    }

    /*
     * (non-Javadoc)
     * @see com.applabs.mydiary.core.IPersister#deinitialize()
     */
    @Override
    public boolean deinitialize() {
        // TODO Auto-generated method stub
        return false;
    }

    /*
     * (non-Javadoc)
     * @see com.applabs.mydiary.core.IPersister#getAllEvents()
     */
    @Override
    public List<DiaryEvent> getAllEvents() {
        // TODO Auto-generated method stub
        ArrayList<DiaryEvent> tdList = new ArrayList<DiaryEvent>();
        Cursor evtCurser = mDbProvider.query(mDbProvider.CONTENT_URI, null, null, null, null);
        if (evtCurser != null) {
            if (evtCurser.moveToFirst()) {
                do {
                    DiaryEvent tdEvent = new DiaryEvent();
                    tdEvent.setEventDate(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_DATE)));
                    tdEvent.setAddress(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_ADDR)));
                    tdEvent.setNotes(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_NOTES)));
                    tdEvent.setEventId(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_ID)));
                    tdEvent.setStartTime(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_STIME)));
                    tdEvent.setEndTime(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_ETIME)));
                    tdEvent.setLatitude(Long.parseLong(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_LATI))));
                    tdEvent.setLongitude(Long.parseLong(evtCurser.getString(evtCurser
                            .getColumnIndex(DiaryEventTable.COLUMN_EVT_LATI))));
                    tdList.add(tdEvent);
                } while (evtCurser.moveToNext());
            }
        }
        evtCurser.close();
        return tdList;
    }

}
